package sample.clients;

import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import sample.entity.User;

public class ModifyUser {
	public static void main(String[] args) {
		System.out.println("modifiyng User");
		
		SessionFactory sessions=new AnnotationConfiguration().configure().buildSessionFactory();
		Session session=sessions.openSession();
		
		session.getTransaction().begin();
		
		User user=(User)session.get(User.class, new Long(2));
		
		// session has methods 'get' and 'load' - for getting objects by id
		// the difference is : if we pass non existing id then
		// get method - returns null
		//load method - throws Exception
		
		
		
		
		user.setName(user.getName()+";");
		
		//session.refresh - reload data from database , so if we make some changes
		//on this object , we can rollback these changes
		//session.refresh(user);
		
		
		
		session.saveOrUpdate(user);
		System.out.println(user.getId()+" "+user.getName());          
		
		session.beginTransaction().commit();
		session.close();
		
	}
}
